package com.boolib.animate.commons.exception;

import lombok.Builder;
import lombok.Data;

import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @author nier
 * @create 2020-02-18 19:44
 */

@ResponseStatus(HttpStatus.BAD_REQUEST)
@RestControllerAdvice
public class MyGlobalExceptionHandler {

    @Builder
    @Data
    public static class ErrorResult{
        public Integer error_code;
        public String error_msg;
    }


    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler({ EmptyResultDataAccessException.class,ItemNotFoundException.class})
    public ErrorResult itemNotExist(){
        return ErrorResult.builder().error_code(101).error_msg("当前项不存在").build();
    }


    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(UserNameExistException.class)
    public ErrorResult userNameExistException(){
        return ErrorResult.builder().error_code(10000).error_msg("当前用户已存在").build();
    }
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(UserAuthenticationException.class)
    public ErrorResult UserAuthenticationException(){
        return ErrorResult.builder().error_code(10001).error_msg("当前用户尚未登录").build();
    }




}
